package com.js.service.bot.core;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.js.common.constant.BotConst;
import com.js.common.constant.Const;
import com.js.domian.entity.bot.CommandEntity;
import com.js.domian.entity.bot.ResponseEntity;
import com.js.domian.entity.bot.WebsocketEntity;
import com.js.domian.model.bot.ApiResult;
import com.js.domian.model.bot.BaseApi;
import com.js.domian.model.bot.analysis.MessageChain;
import com.js.domian.model.bot.analysis.message.AtMessage;
import com.js.domian.model.bot.analysis.message.TextMessage;
import com.js.domian.model.bot.support.SendGroupMsg;
import com.js.service.bot.basics.ResponseService;
import com.js.service.bot.basics.WebsocketService;
import com.js.service.bot.response.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BotGroupService {
    @Resource
    private ResponseService responseService;
    @Resource
    private WebsocketService websocketService;
    @Resource
    private ApplicationContext applicationContext;

    @Async("wsExecutor")
    public void baseSend(WebSocketClient client, Long groupId, Long userId, MessageChain messageChain) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        WebsocketEntity entity = websocketService.getWebsocketByUrl(client.getURI().toString());
        List<ResponseEntity> list = responseService.getResponseListByWebsocketId(entity.getId());
        MessageChain textMessageChain = new MessageChain();
        MessageChain atMessageChain = new MessageChain();
        textMessageChain.addAll(messageChain.stream().filter(message -> message instanceof TextMessage).collect(Collectors.toList()));
        atMessageChain.addAll(messageChain.stream().filter(message -> message instanceof AtMessage).collect(Collectors.toList()));
        String message = textMessageChain.toString().trim();
        String at = atMessageChain.toString().trim().replace("@", "");
        ok:
        for (ResponseEntity response : list) {
            for (CommandEntity command : response.getCommandList()) {
                if(Const.BASE_YES_NO.YES.getValue().equals(command.getAt())){
                    if(!at.equals(String.valueOf(response.getWebsocketQq()))){
                        break ok;
                    }
                }
                boolean success = false;
                switch (command.getCommandType()){
                    case "1":
                        //等于
                        if(message.equals(command.getCommand())){
                            success = true;
                        }
                        break;
                    case "2":
                        //包含
                        if(message.indexOf(command.getCommand()) > -1){
                            success = true;
                        }
                        break;
                    case "3":
                        //起始
                        if(message.startsWith(command.getCommand())){
                            success = true;
                        }
                        break;
                    case "4":
                        //结束
                        if(message.endsWith(command.getCommand())){
                            success = true;
                        }
                        break;
                    case "5":
                        //正则
                        if(ReUtil.isMatch(command.getCommand(), message)){
                            success = true;
                        }
                        break;
                }
                if(success){
                    BaseService baseService = (BaseService) applicationContext.getBean(response.getResponseClass());
                    MessageChain responseMessage = baseService.buildMessage(groupId, userId, message, response);
                    baseService.sendGroupMessage(entity.getQq(), groupId, responseMessage);
                    break;
                }
            }
        }
    }
}
